<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <img src="../../../public/logo.jpg" />
      <p>电商后台管理系统</p>
      <el-button size="mini" type="primary" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? 64 + 'px' : 300 + 'px'">
        <!-- 开关左侧列表 -->
        <div id="showMenu" @click="showMenu">
          |||
        </div>
        <el-menu
          router
          background-color="#333744"
          text-color="#fff"
          active-text-color="#aa00ee"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          default-active="/roles"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
            unique-opened
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icon[item.id]"></i>
              <!-- 标题 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="item1 in item.children"
              :index="item1.id + ''"
              :key="item1.id"
              :route="{ path: item1.path }"
            >
              <i class="el-icon-s-grid"></i>
              {{ item1.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: '',
      icon: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-operation',
        101: 'el-icon-s-shop',
        102: 'el-icon-tickets',
        145: 'el-icon-data-analysis'
      },
      isCollapse: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    //  退出登录
    logout() {
      window.sessionStorage.clear('token')
      this.$router.push('/login')
    },
    //  获取左侧列表
    async getMenuList() {
      const { data } = await this.$http.get('menus')
      console.log(data.data)
      if (data.meta.status !== 200) {
        return this.$message({ message: '获取权限列表失败', type: 'error' })
      }
      console.log('获取权限列表成功')
      // this.$message({ message: '获取权限列表成功', type: 'success' })
      this.menuList = data.data
    },
    //  开关左侧列表
    showMenu() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #373d41;
  img {
    margin-left: 0;
    margin-top: 5px;
    height: 50px;
    width: 50px;
    border-radius: 50%;
  }
  p {
    align-content: center;
    font-size: 20px;
    color: white;
  }
  .el-button {
    margin: 15px;
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu-item i {
  margin-right: 15px;
}
#showMenu {
  color: white;
  text-align: center;
  background-color: rgb(97, 92, 92);
  padding: 3px;
  cursor: pointer;
}
</style>
